import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 约定： 数据格式为 userName=jack&password=123456
 */
@WebServlet("/login")
public class Login extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理登录请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 验证用户的合法性
         if ("jack".equals(username) && "666666".equals(password)){
            // 可以登录
            // 服务器需要能获取到用户信息
            HttpSession session = req.getSession(true);
            session.setAttribute("username",username);
            session.setAttribute("count", 0);// 登录次数
            resp.sendRedirect("index");
        }else{
            // 登陆失败
            resp.sendRedirect("login.html");
        }
    }
}
